#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

long long numberOfRightTriangles(vector<vector<int>>& grid) {
    int n = grid.size(), m = grid[0].size();
    vector<int> row(n), cal(m);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 1)
            {
                row[i]++;
                cal[j]++;
            }

    long long res = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (grid[i][j] == 1)
                res += ((long long)row[i] - 1) * (cal[j] - 1);

    return res;
}